﻿using System.ComponentModel;
using System.Windows.Navigation;
using CuteRibs.MVVMLib;
using CuteRibs.WowArmory.Models;
using CuteRibs.WowArmory.Resources;
using CuteRibs.WowArmory.Views;

namespace CuteRibs.WowArmory.ViewModels
{
	public class SearchPageViewModel : PageViewModelBase
	{
		public AppRes AppRes { get { return App.AppResource; } }
		public AppTitleControlViewModel AppTitle { get; set; }

		private SearchPageView view;
		private string styleKey;
		private SearchResultViewModel result;

		public string StyleKey
		{
			get { return this.styleKey; }
			set
			{
				if (this.styleKey != value)
				{
					this.styleKey = value;
					this.NotifyPropertyChanged("StyleKey");
				}
			}
		}

		public SearchResultViewModel Result
		{
			get { return this.result; }
			set
			{
				if (this.result != value)
				{
					this.result = value;
					this.NotifyPropertyChanged("Result");
				}
			}
		}

		public SearchPageViewModel()
		{
			this.AppTitle = new AppTitleControlViewModel(AppRes.Title_Search, App.RegionData.Region.DisplayName);
			this.StyleKey = "Summary";

			if (App.IsDesignMode)
			{
				App.SelectRegion(Region.Americas);
				this.DoSearch("ghost", SearchField.Character);
			}
		}

		public override void OnNavigatedTo(ViewBase sender, NavigationEventArgs eventArgs, NavigationContext context)
		{
			this.view = sender as SearchPageView;
			base.OnNavigatedTo(sender, eventArgs, context);
		}

		public async void DoSearch(string term, string field = null, int page = 1)
		{
			if (string.IsNullOrWhiteSpace(term))
			{
				App.ShowAlert(AppRes.MsgNoKeyword);
				return;
			}

			this.ShowProgressBar = true;

			switch (field)
			{
				case SearchField.Character:
					this.StyleKey = "Character";
					this.Result = await App.Search<CharacterSearchResultViewModel>(term, field, page);
					break;
				case SearchField.Guild:
					this.StyleKey = "Guild";
					this.Result = await App.Search<GuildSearchResultViewModel>(term, field, page);
					break;
				case SearchField.Item:
					this.StyleKey = "Item";
					this.Result = await App.Search<ItemSearchResultViewModel>(term, field, page);
					break;
				case SearchField.ArenaTeam:
					this.StyleKey = "ArenaTeam";
					this.Result = await App.Search<ArenaTeamSearchResultViewModel>(term, field, page);
					break;
				case SearchField.Content:
					this.StyleKey = "Content";
					this.Result = await App.Search<ContentSearchResultViewModel>(term, field, page);
					break;
				case SearchField.Forum:
					this.StyleKey = "Forum";
					this.Result = await App.Search<ForumSearchResultViewModel>(term, field, page);
					break;
				case SearchField.Blog:
					this.StyleKey = "Blog";
					this.Result = await App.Search<BlogSearchResultViewModel>(term, field, page);
					break;
				case SearchField.Support:
					this.StyleKey = "Support";
					this.Result = await App.Search<SupportSearchResultViewModel>(term, field, page);
					break;
				default:
					this.StyleKey = "Summary";
					this.Result = await App.Search(term);
					break;
			}

			if (!App.IsDesignMode)
			{
				if (this.Result.PageNumber.IsBetween(1, this.Result.PageCount))
				{
					this.view.AppButtons[0].IsEnabled = this.Result.PageNumber > 1;
					this.view.AppButtons[1].IsEnabled = this.Result.PageNumber < this.Result.PageCount;
				}
				else
				{
					this.view.AppButtons[0].IsEnabled =
						this.view.AppButtons[1].IsEnabled = false;
				}
			}

			this.ShowProgressBar = false;
		}

		public void LoadPage(bool isNext)
		{
			var n = isNext ? 1 : -1;
			var newPage = this.Result.PageNumber + n;
			this.DoSearch(this.Result.Term, this.Result.Field, newPage);
		}

		public void NavigateTo(SearchResultItemViewModel vm)
		{
			if (vm == null)
			{
				return;
			}

			if (vm is SummarySearchResultItemViewModel)
			{
				var field = (vm as SummarySearchResultItemViewModel).Field;
				this.DoSearch(this.Result.Term, field);
			}
			else if (vm is CharacterSearchResultItemViewModel)
			{
				var character = vm as CharacterSearchResultItemViewModel;
				App.ShowAlert(character.Slug + character.Name);
			}
			else
			{
				this.Frame.Navigate(vm.Uri);
			}
		}

		public void OnBackKeyPress(CancelEventArgs e)
		{
			if(this.StyleKey != "Summary")
			{
				e.Cancel = true;
				this.DoSearch(this.Result.Term);
			}
		}
	}
}
